package com.atjg.autocat.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.widget.TextView
import com.atjg.autocat.R
import com.atjg.autocat.utils.MusicPlayUtils
import com.atjg.autocat.viewModel.MainViewModel
import com.blankj.utilcode.util.ScreenUtils
import com.lxj.xpopup.core.CenterPopupView
import com.zhy.baselibrary.utils.clickWithTrigger
import kotlinx.coroutines.Job


@SuppressLint("ViewConstructor")
class SuccessDialog(var mContext: Context) :
    CenterPopupView(mContext) {
    private val viewmodel by lazy {
        MainViewModel()
    }
    private var job: Job? = null
    private var tvTime: TextView? = null
    private var tvTip: TextView? = null
    private var tvCancel: TextView? = null
    override fun getImplLayoutId(): Int {
        return R.layout.dialog_success
    }

    @SuppressLint("SetTextI18n")
    override fun onCreate() {
        super.onCreate()
        MusicPlayUtils.playBgM(mContext,R.raw.music_pay_success)
        tvTime = findViewById<TextView>(R.id.tvTime)
        tvTip = findViewById<TextView>(R.id.tvTip)
        tvCancel = findViewById<TextView>(R.id.tvCancel)
        tvCancel?.clickWithTrigger {
            job?.cancel()
            dismiss()
        }
        job = viewmodel.testTime(30000, {
            tvTime?.text = "${it}秒后关闭界面"
        }, {
            tvCancel?.visibility = View.VISIBLE
        })
    }

    @SuppressLint("SetTextI18n")
    fun setSuccess(isSuccess: Boolean) {
        if (isSuccess){
            tvTip?.text = "出货成功"
        }else{
            tvTip?.text = "出货失败，请联系管理员！"
        }
        tvCancel?.visibility = View.VISIBLE
    }

    override fun onDismiss() {
        super.onDismiss()
        job?.cancel()
    }

    interface OnResetClickListener {
        fun onResetClick()
    }

    override fun getMaxWidth(): Int {
        return ScreenUtils.getScreenWidth()
    }

}